// pages/lostUpload/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{ id: 0, value: "发布失物招领", isActive: true }],
    // 被选中的图片路径 数组
    chooseImgs: [],
    tempFilePaths: [],
    userId: 1,
  },
  // 接口返回的的图片的路径数组
  UpLoadImgs: [],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  //点击删除图片
  handleRemoveImg(e) {
    // 2 获取被点击的组件的索引
    const { index } = e.currentTarget.dataset;
    // 3 获取data中的图片数组
    let { chooseImgs } = this.data;
    // 4 删除元素
    chooseImgs.splice(index, 1);
    this.setData({
      chooseImgs,
    });
  },

  // 点击 “+” 选择图片
  handleChooseImg() {
    let that = this;
    // 2 调用小程序内置的选择图片api
    wx.chooseImage({
      // 同时选中的图片的数量
      count: 9,
      // 图片的格式  原图  压缩
      sizeType: ["original", "compressed"],
      // 图片的来源  相册  照相机
      sourceType: ["album", "camera"],

      success: (result) => {
        const tempFilePaths = result.tempFilePaths;
        console.log(444, result);
        this.setData({
          // 图片数组 进行拼接
          chooseImgs: [...this.data.chooseImgs, ...result.tempFilePaths],
          tempFilePaths: result.tempFilePaths,
        });
        wx.uploadFile({
          url: "http://172.17.26.211:3310/uploadFile",
          filePath: tempFilePaths[0],
          name: "file",
          formData: {
            user: "test",
          },
          success(result) {
            //console.log(999, result);
            let url = JSON.parse(result.data);
            //console.log(1000, url.data);
            //console.log(3000, that.testValue);
            // that.setData({ UpLoadImgs: url.data });
            let imgUrl = url.data;
            that.UpLoadImgs.push(imgUrl);
            //  console.log(1100, that.UpLoadImgs);
          },
        });
      },
    });
  },

  // 提交按钮的点击
  async formSubmit(e) {
    //create_time
    //imgs_url
    //last_found_title
    //wx_user_id
    let timing = new Date();
    let formData = e.detail.value;
    formData.create_time = this.fromData(timing);
    formData.wx_user_id = this.data.userId;
    formData.imgs_url = this.UpLoadImgs[0];
    console.log(999, formData);

    const res = await request({
      url: "/lastfound/addLastFound",
      data: formData,
    });

    console.log(1000, res);

    wx.showLoading({
      title: "正在上传中",
      mask: true,
    });
    wx.navigateBack({
      delta: 1,
    });
  },

  //时间戳处理函数，时间戳转换成正常数据
  fromData(a) {
    let time = new Date(a);
    let month = time.getMonth() + 1;
    let date = time.getDate();
    let hours = time.getHours();
    let minutes = time.getMinutes();
    let second = time.getSeconds(); // 获取秒数
    // 小于10前加一个0
    if (month < 10) {
      month = "0" + month;
    }
    if (date < 10) {
      date = "0" + date;
    }
    if (hours < 10) {
      hours = "0" + hours;
    }
    if (minutes < 10) {
      minutes = "0" + minutes;
    }
    if (second < 10) {
      second = "0" + second;
    }
    return (
      time.getFullYear() +
      "-" +
      month +
      "-" +
      date +
      " " +
      hours +
      ":" +
      minutes +
      ":" +
      second
    );
  },
});
